home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / RTrace 1.0 / source / macequations.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-23  |  11.7 KB  |  321 lines  |  [TEXT/KAHL]

  1. /*****************************************************************************\
  2. * macequations.c                                                              *
  3. *                                                                             *
  4. * This file contains code which is specific to the Macintosh.  It contains    *
  5. * code which interfaces with the equation interpreter.                        *
  6. \*****************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "extern.h"
  10. #include "rtresources.h"
  11. #include "interp_equation.h"
  12.  
  13. /* The equations */
  14.  
  15. char    x_up_eq[255];
  16. char    y_up_eq[255];
  17. char    z_up_eq[255];
  18. char    x_look_eq[255];
  19. char    y_look_eq[255];
  20. char    z_look_eq[255];
  21. char    x_eye_eq[255];
  22. char    y_eye_eq[255];
  23. char    z_eye_eq[255];
  24. char    x_angle_eq[255];
  25. char    y_angle_eq[255];
  26.  
  27.  
  28. /* Externals */
  29. extern DialogPtr    animation_dialog;        /* the animation dialog */
  30. extern real            t;                        /* the current value of t */
  31.  
  32. /* Protypes */
  33. Boolean find_variable_value (char *var_name, real *value);
  34. void setup_frame(void);
  35. void get_animation_info(real *tstart, real *tend, long *frames);
  36.  
  37.  
  38.  
  39. /*****************************************************************************\
  40. * procedure get_animation_info                                                *
  41. *                                                                             *
  42. * Purpose: This procedure gets the animation parameters from the animation    *
  43. *          dialog.                                                            *
  44. *                                                                             *
  45. * Parameters: tstart: receives the starting t value                           *
  46. *             tend:   receives the ending t value                             *
  47. *             frames: receives the number of frames                              *
  48. *             the global equation variables are also set.                     *
  49. *                                                                             *
  50. * Created by: Greg Ferrar                                                     *
  51. * Created on: September 9, 1992                                               *
  52. * Modified:                                                                   *
  53. \*****************************************************************************/
  54.  
  55. void get_animation_info(real *tstart, real *tend, long *frames)
  56. {
  57.  
  58.     short    type;                    /* the item type of a DITL item */
  59.     Handle    handle;                    /* the handle to a DITL item */
  60.     Rect    box;                    /* the bounding box of a DITL item */
  61.     Str255    text;                    /* the text of a DITL item */
  62.     short    error;                    /* error code of detected error */
  63.     static char    error_message[200];    /* error message of detected error */
  64.  
  65.     /* Get the starting t value */
  66.     GetDItem (animation_dialog, T_RANGES_FROM_EQ, &type, &handle, &box);
  67.     GetIText (handle, text);
  68.     if (error = evaluate_equation(PtoCstr(text), tstart, find_variable_value))
  69.         {
  70.         strcpy(error_message, "Error in starting t value: ");
  71.         strcat(error_message, get_error_message(error));
  72.         abortive_string_error(error_message);
  73.         }
  74.  
  75.     /* Get the ending t value */
  76.     GetDItem (animation_dialog, T_RANGES_TO_EQ, &type, &handle, &box);
  77.     GetIText (handle, text);
  78.     if (error = evaluate_equation(PtoCstr(text), tend, find_variable_value))
  79.         {
  80.         strcpy(error_message, "Error in starting t value: ");
  81.         strcat(error_message, get_error_message(error));
  82.         abortive_string_error(error_message);
  83.         }
  84.  
  85.     /* Get the number of frames */
  86.     GetDItem (animation_dialog, NUM_FRAMES_NUM, &type, &handle, &box);
  87.     GetIText (handle, text);
  88.     StringToNum (text, frames);
  89.  
  90.     /* Get the x eye equation */
  91.     GetDItem (animation_dialog, X_EYE_EQ, &type, &handle, &box);
  92.     GetIText (handle, x_eye_eq);
  93.     PtoCstr(x_eye_eq);
  94.  
  95.     /* Get the y eye equation */
  96.     GetDItem (animation_dialog, Y_EYE_EQ, &type, &handle, &box);
  97.     GetIText (handle, y_eye_eq);
  98.     PtoCstr(y_eye_eq);
  99.  
  100.     /* Get the z eye equation */
  101.     GetDItem (animation_dialog, Z_EYE_EQ, &type, &handle, &box);
  102.     GetIText (handle, z_eye_eq);
  103.     PtoCstr(z_eye_eq);
  104.  
  105.     /* Get the x look equation */
  106.     GetDItem (animation_dialog, X_LOOK_EQ, &type, &handle, &box);
  107.     GetIText (handle, x_look_eq);
  108.     PtoCstr(x_look_eq);
  109.  
  110.     /* Get the y look equation */
  111.     GetDItem (animation_dialog, Y_LOOK_EQ, &type, &handle, &box);
  112.     GetIText (handle, y_look_eq);
  113.     PtoCstr(y_look_eq);
  114.  
  115.     /* Get the z look equation */
  116.     GetDItem (animation_dialog, Z_LOOK_EQ, &type, &handle, &box);
  117.     GetIText (handle, z_look_eq);
  118.     PtoCstr(z_look_eq);
  119.  
  120.     /* Get the x up equation */
  121.     GetDItem (animation_dialog, X_UP_EQ, &type, &handle, &box);
  122.     GetIText (handle, x_up_eq);
  123.     PtoCstr(x_up_eq);
  124.  
  125.     /* Get the y up equation */
  126.     GetDItem (animation_dialog, Y_UP_EQ, &type, &handle, &box);
  127.     GetIText (handle, y_up_eq);
  128.     PtoCstr(y_up_eq);
  129.  
  130.     /* Get the z up equation */
  131.     GetDItem (animation_dialog, Z_UP_EQ, &type, &handle, &box);
  132.     GetIText (handle, z_up_eq);
  133.     PtoCstr(z_up_eq);
  134.  
  135.     /* Get the x angle equation */
  136.     GetDItem (animation_dialog, ANGLE_X_EQ, &type, &handle, &box);
  137.     GetIText (handle, x_angle_eq);
  138.     PtoCstr(x_angle_eq);
  139.  
  140.     /* Get the y angle equation */
  141.     GetDItem (animation_dialog, ANGLE_Y_EQ, &type, &handle, &box);
  142.     GetIText (handle, y_angle_eq);
  143.     PtoCstr(y_angle_eq);
  144.  
  145. }    /* get_animation_info() */
  146.  
  147.  
  148.  
  149. /*****************************************************************************\
  150. * procedure setup_frame                                                       *
  151. *                                                                             *
  152. * Purpose: This procedure sets up the eye and view points, and the up vector, *
  153. *          using the current value of t.                                      *
  154. *                                                                             *
  155. * Created by: Greg Ferrar                                                     *
  156. * Created on: September 11, 1992                                              *
  157. * Modified:                                                                   *
  158. \*****************************************************************************/
  159.  
  160. void setup_frame(void)
  161. {
  162.  
  163.     char    error_message[100];
  164.     short    error;
  165.  
  166.     /* Find the x eye value, check for errors */
  167.     if (error = evaluate_equation(x_eye_eq, &eye.x, find_variable_value))
  168.         {
  169.         strcpy(error_message, "Error evaluating eye x equation: ");
  170.         strcat(error_message, get_error_message(error));
  171.         abortive_string_error(error_message);
  172.         }
  173.     
  174.     /* Find the y eye value, check for errors */
  175.     if (error = evaluate_equation(y_eye_eq, &eye.y, find_variable_value))
  176.         {
  177.         strcpy(error_message, "Error evaluating eye y equation: ");
  178.         strcat(error_message, get_error_message(error));
  179.         abortive_string_error(error_message);
  180.         }
  181.         
  182.     /* Find the z eye value, check for errors */
  183.     if (error = evaluate_equation(z_eye_eq, &eye.z, find_variable_value))
  184.         {
  185.         strcpy(error_message, "Error evaluating eye z equation: ");
  186.         strcat(error_message, get_error_message(error));
  187.         abortive_string_error(error_message);
  188.         }
  189.  
  190.     /* Find the x look value, check for errors */
  191.     if (error = evaluate_equation(x_look_eq, &look.x, find_variable_value))
  192.         {
  193.         strcpy(error_message, "Error evaluating look x equation: ");
  194.         strcat(error_message, get_error_message(error));
  195.         abortive_string_error(error_message);
  196.         }
  197.         
  198.     /* Find the y look value, check for errors */
  199.     if (error = evaluate_equation(y_look_eq, &look.y, find_variable_value))
  200.         {
  201.         strcpy(error_message, "Error evaluating look y equation: ");
  202.         strcat(error_message, get_error_message(error));
  203.         abortive_string_error(error_message);
  204.         }
  205.         
  206.     /* Find the z look value, check for errors */
  207.     if (error = evaluate_equation(z_look_eq, &look.z, find_variable_value))
  208.         {
  209.         strcpy(error_message, "Error evaluating look z equation: ");
  210.         strcat(error_message, get_error_message(error));
  211.         abortive_string_error(error_message);
  212.         }
  213.  
  214.     /* Find the x up value, check for errors */
  215.     if (error = evaluate_equation(x_up_eq, &up.x, find_variable_value))
  216.         {
  217.         strcpy(error_message, "Error evaluating up x equation: ");
  218.         strcat(error_message, get_error_message(error));
  219.         abortive_string_error(error_message);
  220.         }
  221.         
  222.     /* Find the y up value, check for errors */
  223.     if (error = evaluate_equation(y_up_eq, &up.y, find_variable_value))
  224.         {
  225.         strcpy(error_message, "Error evaluating up y equation: ");
  226.         strcat(error_message, get_error_message(error));
  227.         abortive_string_error(error_message);
  228.         }
  229.         
  230.     /* Find the z up value, check for errors */
  231.     if (error = evaluate_equation(z_up_eq, &up.z, find_variable_value))
  232.         {
  233.         strcpy(error_message, "Error evaluating up z equation: ");
  234.         strcat(error_message, get_error_message(error));
  235.         abortive_string_error(error_message);
  236.         }
  237.  
  238.     /* Find the x angle, check for errors */
  239.     if (error = evaluate_equation(x_angle_eq, &view_angle_x, find_variable_value))
  240.         {
  241.         strcpy(error_message, "Error evaluating x angle equation: ");
  242.         strcat(error_message, get_error_message(error));
  243.         abortive_string_error(error_message);
  244.         }
  245.     view_angle_x = DEGREE_TO_RADIAN(view_angle_x);
  246.         
  247.     /* Find the y angle, check for errors */
  248.     if (error = evaluate_equation(y_angle_eq, &view_angle_y, find_variable_value))
  249.         {
  250.         strcpy(error_message, "Error evaluating y angle equation: ");
  251.         strcat(error_message, get_error_message(error));
  252.         abortive_string_error(error_message);
  253.         }
  254.     view_angle_y = DEGREE_TO_RADIAN(view_angle_y);
  255.  
  256.     /* Recomputer the gaze for this new eye point */
  257.     gaze.x = look.x - eye.x;
  258.     gaze.y = look.y - eye.y;
  259.     gaze.z = look.z - eye.z;
  260.     gaze_distance = LENGTH(gaze);
  261.     
  262.     /* If we're right on top of the look point, it's an error */
  263.     if (gaze_distance < ROUNDOFF)
  264.         {
  265.         sprintf(error_message, "Eye point equal to look point when t = %lg", t);
  266.         abortive_string_error(error_message);
  267.         }
  268.         
  269.     /* Otherwise, we can normalize the gaze */
  270.     NORMALIZE(gaze);
  271.  
  272.     /* If the up vector is null, it's an error */
  273.     if (LENGTH(up) < ROUNDOFF)
  274.         {
  275.         sprintf(error_message, "Up vector is zero when t = %lg", t);
  276.         abortive_string_error(error_message);
  277.         }
  278.     
  279.     /* Otherwise, we can normalize the up vector */
  280.     NORMALIZE(up);
  281.         
  282.     /* check for bad up vector */
  283.     if (ABS(DOT_PRODUCT(gaze, up)) > COS(ANGLE_MIN))
  284.         {
  285.         sprintf(error_message, "Bad up vector when t = %lg", t);
  286.         abortive_string_error(error_message);
  287.         }
  288.                 
  289. }    /* setup_frame() */
  290.  
  291.  
  292.  
  293. /*****************************************************************************\
  294. * procedure find_variable_value                                               *
  295. *                                                                             *
  296. * Purpose: This procedure is called when the equation interpreter wants to    *
  297. *          know the value of a variable.                                      *
  298. *                                                                             *                                                                             *
  299. * Parameters: var_name: the name of the variable.                             *                                                                             *
  300. *             value:    receives the valud of the variable.                   *                                                                             *
  301. *             returns TRUE if variable is undefined.                          *                                                                             *
  302. *                                                                             *                                                                             *
  303. * Created by: Greg Ferrar                                                     *
  304. * Created on: September 9, 1992                                               *
  305. * Modified:                                                                   *
  306. \*****************************************************************************/
  307.  
  308. Boolean find_variable_value (char *var_name, real *value)
  309. {
  310.  
  311.     /* We only know t.  If it's t, use the current value.  Otherwise, it's
  312.         an error */
  313.     if (!strcmp(var_name, "t"))
  314.         {
  315.         *value = t;
  316.         return FALSE;
  317.         }
  318.     else
  319.         return TRUE;
  320.  
  321. }